<?php
//SVG  compressed with gzip have the extension ".svgz"
/*  return'<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">';
*/
/*	<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"   "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve"
	 width="200px" height="50px" viewBox="0 0 200 50" zoomAndPan="disable" >
	 //////////////////////////////////////////////////////////////magnify
</svg>*/


include "babs.php";

function mbox($s){ echo "<script>alert(\"$s\");</script>";
}

function savesvg($filename,$svg){
  pfile($filename,$svg);
  echo"<iframe src=\"$filename\" width=\"800\" height=\"600\" />";
  //echo'<meta http-equiv="refresh" content="0;url=',$svgfile,'" />';
}

function svgstart(){
  return'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">';
}

function svgstop(){
  return svgclose().'</svg>';
}
function svgclose($newsvg=''){ return''; }


function paersvg($babsvg){
  $lines=explode(';',$babsvg);
  $c=count($lines);
  $i=0;
  $svg=array('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="800" height="600">');
  $shape=false;



  while($i<$c){
    $line=trim($lines[$i]);
    switch($line){
	  case'R':
	    $shape=$line; //echo'start RECT'.pbr();
	    $svg[]='<rect x="'.$lines[++$i].'" y="'.$lines[++$i].'" width="'.$lines[++$i].'" height="'.$lines[++$i].'" style="'.$lines[++$i].'">';
		break;
	  case'A':
		$svg[]='<a xlink:href="'.$lines[++$i].'" target="'.$lines[++$i].'">';
	  case'AN':
	    $svg[]='<animate attributeName="'.$lines[++$i].'" attributeType="XML" begin="'.$lines[++$i].'s" dur="'.$lines[++$i].'s" fill="freeze" from="'.$lines[++$i].'" to="'.$lines[++$i].'" />';
	    break;
	  case'AC':
	    $svg[]='<animateColor attributeName="'.$lines[++$i].'" attributeType="CSS" begin="'.$lines[++$i].'s" dur="'.$lines[++$i].'s" fill="freeze" from="'.$lines[++$i].'" to="'.$lines[++$i].'" />';
	    break;
	  case'I':
	    $svg[]='<image x="'.$lines[++$i].'" y="'.$lines[++$i].'" width="'.$lines[++$i].'" height="'.$lines[++$i].'" xlink:href="'.$lines[++$i].'" />';
		echo $lines[$i];
		break;
	  case'a': $svg[]='</a>'; ++$i; break;
	  case'r': $svg[]='</rect>'; ++$i; break;
	  default:
	    //echo'**'.$line.'**<br />';
	    ++$i;
    }
  }
  $svg[]='</svg>';
  echo'lala>>'.htmlentities(implode($svg));
  return implode($svg);
}
$nitrixsvg='
R;10;10;80;80;;r;
R;100;0;200;200;;r;
';

$trixsvg='
I;0;0;800;600;../wax/kevin/site/images/logo.png;
A;http://www.google.com;_blank;
I;0;0;800;600;../wax/kevin/site/images/socials.png;
a;



';
$ntrixsvg='
I;20;20;298;65;http://jenkov.com/images/layout/top-bar-logo.png;
R;0;0;500;40;fill:orange;
AN;x;0;6;300;0;
AN;y;0;6;100;0;
AN;width;0;6;300;800;
AN;height;0;6;100;300;
AC;fill;2;4;lime;red;
r

R;0;0;20;20;fill:blue;r
';
$test=paersvg($nitrixsvg);

$nitrix='
<svg xmlns="http://www.w3.org/2000/svg"      xmlns:xlink="http://www.w3.org/1999/xlink">
<image x="20" y="20" width="298" height="65"
   xlink:href="http://jenkov.com/images/layout/top-bar-logo.png" />
<rect x="20" y="20" width="500" height="40" style="fill:orange">
<animate attributeName="x" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="0"/>
<animate attributeName="y" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="0"/>
<animate attributeName="width" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="800"/>
<animate attributeName="height" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="300"/>
<animateColor attributeName="fill" attributeType="CSS" from="lime" to="red" begin="2s" dur="4s" fill="freeze"/>
</rect>
<rect x="10" y="500" width="80" height="700" style="fill:green">
<animate attributeName="x" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="0"/>
<animate attributeName="y" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="0"/>
<animate attributeName="width" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="800"/>
<animate attributeName="height" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="300"/>
<animateColor attributeName="fill" attributeType="CSS" from="lime" to="red" begin="2s" dur="4s" fill="freeze"/>
</rect>
<image x="20" y="20" width="298" height="65"    xlink:href="http://jenkov.com/images/layout/top-bar-logo.png" />
<image x="400" Y="40" width="200" height="200" xlink:href="../wax/kevin/site/images/logo.png" />
<image x="40" Y="40" xlink:href="../wax/kevin/site/images/logo.png" />
</svg>
';

pbr();

$svgfile='renata.svg';
pfile($svgfile,$test);
echo plink('renata.svg','','i1');
echo"<iframe src=\"$svgfile\" width=\"800\" height=\"600\" />";


/*
<animate attributeName attributeType begin dur fill from to>
			x				XML			s	  s	 freeze .  .
			y
			width
			height
			fill*/
global $svgtag; //current SVG tag



function svgclose($newsvg=''){
  global $svgtag;
  $choice=$svgtag;
  $svgtag=$newsvg;
  switch ($choice){
    case 'rect': return'</rect>'; break;
    case 'circ': return'</circ>'; break;
  }
}

function svgstop(){
  return svgclose().'</svg>';
}

function svgrect($x,$y,$width,$height,$style){
  global $svgtag;
	return svgclose('rect').'<rect x="'.$x.'" y="'.$y.'" width="'.$width.'" height="'.$height.'" style="'.$style.'">';
}

function svgcirc($x,$y,$width,$height,$style){
  global $svgtag;
  return svgclose('circ').'<circ x="'.'$x." y="'.$y.'" width="'.$width.'" height="'.$height.'" style="'.$style.'">';
}

function svganim(){
  return'<animate attributeName="x" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="0"/>
<animate attributeName="y" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="0"/>
<animate attributeName="width" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="800"/>
<animate attributeName="height" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="300"/>
<animateColor attributeName="fill" attributeType="CSS" from="lime" to="red" begin="2s" dur="4s" fill="freeze"/>';
}

function closesvg($tag){
if (substr($tag,0,1)=='<') return '></'.substr($tag).'>';
else return'';
}
function babsvg($babs){
  $points=array('x','y','width','height');
  $maxpoints=count($points);
  $log=$maxpoints;

  //$closesvg=create_function()

  if(is_array($babs)){ echo'reading babsvg ARRAY <br />'; }
  else{echo'reading empty babs<br />'; $babs=array('');}
  $cc=0; $isbabs=true;
  $t='';
  foreach($babs as $ae){
    if(is_string($ae)) $isbabs=true;
    $cc=0;
    $csvg='';

    echo '&aelig;';
    while($cc<strlen($ae)){
      $ch=$ae[$cc];
  	  switch ($ch){
  	  case'C':
	    $t.=closesvg($csvg);
	    $csvg='<circle ';
	    $t.=$csvg;
	  break;
	  case'R': $csvg='';
      default: echo"<u>$ch</u>";
	  }
	  ++$cc;
    }
    $t.=closesvg($csvg);
    echo"<br />";
    echo htmlentities($t);
    echo"<br />";
  }
unset($cc);

$test=implode(array(
  svgstart(),
  svgrect(20,20,500,40,'fill:orange'),
  svganim(),
  svgrect(10,500,80,700,'fill:green'),
  svganim(),
  svgstop()
));

  $f="test.svg";
  $handle=fopen($f, 'w');
  fwrite($handle,$test);
  //echo "htmlenteties rect:".htmlentities(svgrect()).'<br />';
  echo '<br />';
  echo htmlentities(svganim());
  echo'Data Written';
  fclose($handle);
  //mbox($log);
  echo'<div style="background-color:black; width:800; height:600">';
  echo'<img src="test.svg" alt="svgtest" width="200" height="200">';
  echo'<meta http-equiv="refresh" content="0;url=test.svg" />';
  echo'</div>';
}
babsvg(array(
  'R 20 20 250 250 fill:blue
    x:0 6 300 0
    y:0 6 100 0
    w:0 6 300 800
    h:0 6 100 300
    c:2 4 lime red
  ',
  'C 400 300
  '
));

?>